package com.xidian.zhiancloud.controller.emergencyManagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.constant.EmergencyManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.EmergencyDivisionOfTasksMapper;
import com.xidian.zhiancloud.dao.EmergencyPlanManagementMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dto.emergencyManagement.EmergencyDrillPlanDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.EmergencyDivisionOfTasks;
import com.xidian.zhiancloud.model.EmergencyDrillPlan;
import com.xidian.zhiancloud.model.EmergencyPlanManagement;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.emergencyManagement.EmergencyDrillPlanQO;
import com.xidian.zhiancloud.service.emergencyManagement.EmergencyDrillPlanService;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyDivisionOfTasksVO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyDrillPlanDTOVO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyDrillPlanVO;
import com.xidian.zhiancloud.vo.emergencyManagement.EmergencyPlanManagementVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应急管理 应急演练计划接口
 */
@RestController
@RequestMapping("/EmergencyDrillPlan")
public class EmergencyDrillPlanController extends BaseController {

    @Autowired
    private EmergencyDrillPlanService emergencyDrillPlanService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private EmergencyPlanManagementMapper emergencyPlanManagementMapper;

    @Autowired
    private EmergencyDivisionOfTasksMapper emergencyDivisionOfTasksMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 增加应急演练计划
     * @param emergencyDrillPlanDTO 应急演练计划对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(EmergencyDrillPlanDTO emergencyDrillPlanDTO){
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCompanyId(getSessionUserCompanyId());
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCreatorId(getSessionUserid());
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCreateTime(new Date(System.currentTimeMillis()));
        Boolean res=emergencyDrillPlanService.add(emergencyDrillPlanDTO);
        return ResultDTO.successOf(res,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取应急演练计划
     * @param id 应急演练计划对象的id
     * @return 应急演练计划对象
     */
    @GetMapping("/getById")
    public ResultDTO<EmergencyDrillPlanDTOVO> getById(@RequestParam("id") Long id){
        return ResultDTO.successOf(emergencyDrillPlanService.getById(id),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除应急演练计划
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids){
        Boolean res=emergencyDrillPlanService.deleteByIds(ids);
        return ResultDTO.successOf(res,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改应急演练计划
     * @param emergencyDrillPlanDTO 应急演练计划对象
     * @return 返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(EmergencyDrillPlanDTO emergencyDrillPlanDTO) {
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCompanyId(null);
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCreatorId(null);
        emergencyDrillPlanDTO.getEmergencyDrillPlan().setCreateTime(null);
        Boolean res=emergencyDrillPlanService.patchById(emergencyDrillPlanDTO);
        return ResultDTO.successOf(res,GeneralResponseEnums.SUCCESS);
    }


    /**
     * 条件查询
     * @param emergencyTeamManagementQO 查询条件
     * @return 返回查询列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<EmergencyDrillPlanVO>> getListByQueryObject(EmergencyDrillPlanQO emergencyTeamManagementQO) {
        if (emergencyTeamManagementQO.getCurrent() == null || emergencyTeamManagementQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        emergencyTeamManagementQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(emergencyDrillPlanService.getListByQO(emergencyTeamManagementQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     *
     * @param response
     * @param ids      id列表
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        List<EmergencyDrillPlan> list=emergencyDrillPlanService.listByIds(Arrays.asList(ids));
        List<EmergencyDrillPlanVO> ans=new LinkedList<>();
        list.forEach(o -> {
            EmergencyDrillPlanVO vo = new EmergencyDrillPlanVO();
            BeanUtils.copyProperties(o, vo);
            EmergencyPlanManagement plan=emergencyPlanManagementMapper.selectById(vo.getNameRId());
            vo.setNameEmerge(plan==null?"该应急预案不存在":plan.getName());
            Organization org = organizationService.getById(vo.getOrganizationId());
            vo.setOrganizationName(org == null ? "该部门不存在" : org.getName());
            vo.setDrillTypeName(EmergencyManagementConstant.EmergencyDrillType.get(vo.getTypeId()));
            QueryWrapper<EmergencyDivisionOfTasks> emergencyDivisionOfTasksQueryWrapper = new QueryWrapper<EmergencyDivisionOfTasks>();
            emergencyDivisionOfTasksQueryWrapper.eq("emergency_drill_id",o.getId());
            List<EmergencyDivisionOfTasks> emergencyDivisionOfTasksList=emergencyDivisionOfTasksMapper.selectList(emergencyDivisionOfTasksQueryWrapper);
            List<String> voList=new LinkedList<>();
            for (int i = 0; i < emergencyDivisionOfTasksList.size(); i++) {
                EmergencyDivisionOfTasksVO vos=new EmergencyDivisionOfTasksVO();
               BeanUtils.copyProperties(emergencyDivisionOfTasksList.get(i),vos);
                SysUser userid =sysUserMapper.selectById(vos.getNameId());
//                vos.setPersonnelName(userid==null?"该用户不存在":userid.getName());
                voList.add(userid.getName());
            }
            vo.setParticipantNames(StringUtils.concatStrings(voList));
            ans.add(vo);
        });
        ExcelUtils.exportExcel(ans,"应急演练计划","应急演练计划", EmergencyDrillPlanVO.class,"应急演练计划",response);
    }
}
